package com.zcf.service.impl;

import com.zcf.pojo.Dining;
import com.zcf.pojo.Evaluate;
import com.zcf.pojo.Log;
import com.zcf.pojo.Orderinfo;
import com.zcf.pojo.Orders;
import com.zcf.pojo.Riding;
import com.zcf.pojo.RidingOrder;
import com.zcf.pojo.User;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.mapper.DiningMapper;
import com.zcf.mapper.EvaluateMapper;
import com.zcf.service.EvaluateService;
import com.zcf.utils.Hutool;
import com.zcf.utils.UploadImgUtils;
import com.zcf.vo.in.PageVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-12-11
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {

	@Autowired
	EvaluateMapper evaluateMapper;

	@Autowired
	DiningMapper diningMapper;

	// type 0 no匿名 1 yes匿名
	@Override
	public Body insertEvalua(Evaluate evaluate, MultipartFile[] file) throws Exception {
		User user = new User();
		user.setId(evaluate.getUser_id());
		user = user.selectById();
		if (user != null) {
			Dining dining = new Dining();
			EntityWrapper<Dining> awe = new EntityWrapper<>();
			awe.eq("id", evaluate.getDinId());
			dining = dining.selectOne(awe);
			if (dining == null) {
				return Body.newInstance(-1, "商家不存在");
			}
			Orderinfo orders = new Orderinfo();
			orders.setOrderInfoId(evaluate.getOrder_id());
			orders = orders.selectById();
			if (orders == null) {
				return Body.newInstance(-1, "订单不存在");
			}
			RidingOrder ridingOrder = new RidingOrder();
			EntityWrapper<RidingOrder> rin = new EntityWrapper<>();
			rin.eq("order_id", orders.getOrderInfoId());
			ridingOrder = ridingOrder.selectOne(rin);
			Riding riding = new Riding();

			if (ridingOrder != null) {
				evaluate.setRidingId(ridingOrder.getRidingId());
				//return Body.newInstance(-1, "订单不存在");

				if (ridingOrder.getRidingId() != null) {
					EntityWrapper<Riding> ew = new EntityWrapper<>();
					ew.eq("user_id", ridingOrder.getRidingId());
					riding = riding.selectOne(ew);
					if (riding == null) {
						return Body.newInstance(-1, "骑手不存在");
					} else {

					}
				}
			}
			if (file != null && file.length > 0) {
				String url = UploadImgUtils.uploadFiles(file);
				String[] path = url.split(",");
				for (int i = 0; i < path.length; i++) {
					evaluate.addImg(path[i]);
				}
			}
			if (evaluate.getType() == 0) {
				evaluate.setTitle("顾客匿名:");
			} else {
				evaluate.setTitle("顾客" + user.getName() + ":");
			}
			Boolean flag = evaluate.insert();
			if (flag) {
				Orderinfo orderinfo = new Orderinfo();
				orderinfo.setOrderInfoId(evaluate.getOrder_id());
				orderinfo.setStatus(3);
				orderinfo.setDinStatus(3);
				orderinfo.updateById();

				Log log = new Log();
				// 商家用户id
				log.setUserId(dining.getUserId());
				log.setLogDesc("商家评价");
				if (evaluate.getDinClass() == 1 || evaluate.getDinClass() == 2) {
					log.setLogName("获取差评");
				} else if (evaluate.getDinClass() == 3 || evaluate.getDinClass() == 4) {
					log.setLogName("中评");
				} else if (evaluate.getDinClass() == 5) {
					log.setLogName("获取五星好评一次");
				} else {
					log.setLogName("获取五星好评一次");
				}
				log.setAddtime(Hutool.parseDateToStringMM());
				log.setLogId(Hutool.getId());
				log.insert();

				Evaluate evaluate3 = new Evaluate();
				EntityWrapper<Evaluate> as = new EntityWrapper<>();
				as.eq("din_id", evaluate.getDinId());
				int sum = evaluate3.selectCount(as);

				EntityWrapper<Evaluate> ew = new EntityWrapper<>();
				ew.eq("din_id", evaluate.getDinId());
				List<Evaluate> lists = evaluate.selectList(ew);
				if (lists.size() > 0) {
					int num = 0;
					for (Evaluate evaluate2 : lists) {
						num += evaluate2.getDinClass();
					}
					double ast = num / sum;
					// 计算 星级
					BigDecimal classe = new BigDecimal(ast).setScale(0, BigDecimal.ROUND_HALF_UP);
					Dining dining2 = new Dining();
					dining2.setId(evaluate.getDinId());
					dining2.setGrade(classe + "");
					dining2.updateById();
					Log log2 = new Log();
					if (ridingOrder != null) {

						// 骑手用户id
						log2.setUserId(riding.getUserId());
						log2.setAddtime(Hutool.parseDateToStringMM());
						log2.setLogId(Hutool.getId());
						log2.setLogName("骑手评价");

						if (evaluate.getDinClass() == 1 || evaluate.getDinClass() == 2) {
							log2.setLogName("获取差评");
						} else if (evaluate.getDinClass() == 3 || evaluate.getDinClass() == 4) {
							log2.setLogName("中评");
						} else if (evaluate.getDinClass() == 5) {
							log2.setLogName("获取五星好评一次");
						}
						log2.insert();
						Evaluate riding2 = new Evaluate();
						EntityWrapper<Evaluate> asa = new EntityWrapper<>();
						asa.eq("riding_id", evaluate.getRidingId());
						List<Evaluate> list = riding2.selectList(asa);

						EntityWrapper<Evaluate> asaz = new EntityWrapper<>();
						asaz.eq("riding_id", evaluate.getRidingId());
						int asum = riding2.selectCount(asaz);
						int anum = 0;
						for (Evaluate riding3 : list) {
							anum += riding3.getRidingClass();
						}
						double asums = asum;
						double anums = anum;
						double pinjun = anums / asums;
						BigDecimal pinjunzhi = new BigDecimal(pinjun).setScale(0, BigDecimal.ROUND_HALF_UP);
						Riding riding3 = new Riding();
						riding3.setId(riding.getId());
						riding3.setGrades(Integer.parseInt(pinjunzhi + ""));
						riding3.updateById();
					}

				}
				return Body.newInstance(1, "评论成功");
			}
			return Body.newInstance(-1, "评论失败");
		}
		return Body.newInstance(-1, "用户不存在");
	}

	public static void main(String[] args) {
		System.out.println("四舍五入取整:(2)=" + new BigDecimal("1.9").setScale(0, BigDecimal.ROUND_HALF_UP));
		System.out.println("四舍五入取整:(2.4)=" + new BigDecimal("2.4").setScale(0, BigDecimal.ROUND_HALF_UP));
		System.out.println("四舍五入取整:(2.5)=" + new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
		System.out.println("四舍五入取整:(2.9)=" + new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));
		System.out.println(Hutool.parseDateToStringMM());
	}

	@Override
	public Boolean deleteEvalua(String evaluateId) {
		EntityWrapper<Evaluate> ew = new EntityWrapper<>();
		ew.eq("evaluate_id", evaluateId);
		return this.delete(ew);
	}

	@Override
	public ResultVo queryEvalua(String userId, String dinId, PageVo pv) {
		Evaluate evaluate = new Evaluate();
		EntityWrapper<Evaluate> ew = new EntityWrapper<>();
		if (dinId != null) {
			ew.eq("din_id", dinId);
		}
		if (userId != null) {
			ew.eq("user_id", userId);
		}
		ew.orderBy("addtime", false);
		List<Evaluate> list = evaluate.selectList(ew);
		List<Evaluate> lists = new ArrayList<>();
		for (Evaluate evaluate2 : list) {
			if (evaluate2.getRidingId() != null) {
				Riding riding = new Riding();
				riding.setId(evaluate2.getEvaluateId());
				riding = riding.selectById();
				if (riding != null) {
					evaluate2.setName(riding.getName());
				}
			}
			if (evaluate2.getDinId() != null) {
				Dining dining = new Dining();
				dining.setId(evaluate2.getDinId());
				dining = dining.selectById();
				if (dining != null) {
					evaluate2.setDining_name(dining.getDiningName());
				}
			}
			lists.add(evaluate2);

		}
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(lists);
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());
		return rv;
	}

}
